﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HeadLinx.AD.Common;
using System.Diagnostics;
using System.Threading;
using HeadLinx.AD.Explorer.Properties;

namespace HeadLinx.AD.Explorer
{
    /// <summary>
    /// Interaction logic for CreateChartDialog.xaml
    /// </summary>
    public partial class CreateChartDialog : Window
    {
        private HeadLinxGenerator _headLinxGen = new HeadLinxGenerator();
        private HeadLinxPhotoCache _photoCache;
        private HeadLinxQueryType _queryType;
        private HeadLinxUser _rootUser;
        private Exception _loadException = null;

        public CreateChartDialog(HeadLinxPhotoCache photoCache, HeadLinxQueryType queryType)
        {
            InitializeComponent();
            _photoCache = photoCache;
            _queryType = queryType;
            _headLinxGen.MaxCachedPhotoAgeDays = Settings.Default.CachedPhotoExpireDays;
            _headLinxGen.UseJpegPhoto = Settings.Default.UseJpegPhoto;
            _headLinxGen.StatusMessage += new StatusMessageHandler(_headLinxGen_StatusMessage);
            _headLinxGen.GenerateCompletion += new GenerateCompletionHandler(_headLinxGen_GenerateCompletion);
        }

        public bool ShowPeers
        {
            get;
            set;
        }

        public bool ShowDirectReports
        {
            get;
            set;
        }

        public bool ShowPhotos
        {
            get;
            set;
        }

        public bool ForcePhotoCacheRefresh
        {
            get;
            set;
        }

        public string SelectedNames
        {
            get;
            set;
        }

        public HeadLinxUser RootUser
        {
            get
            {
                return _rootUser;
            }
        }

        public Exception LoadException
        {
            get
            {
                return _loadException;
            }
        }

        private void _headLinxGen_GenerateCompletion(HeadLinxUser user, Exception ex)
        {
            if (this.Dispatcher.Thread == Thread.CurrentThread)
            {
                if (user != null)
                {
                    if (_headLinxGen.UnresolvedUserNames.Count != 0)
                    {
                        if (_headLinxGen.UnresolvedUserNames.Count != 0)
                        {
                            StringBuilder sb = new StringBuilder("The following names could not be resolved and will not be included in the chart:");
                            sb.AppendLine();

                            foreach (string name in _headLinxGen.UnresolvedUserNames)
                            {
                                sb.AppendLine(name);
                            }

                            MessageBox.Show(sb.ToString(), "Unresolved names");
                        }
                    }

                    _rootUser = user;
                    this.DialogResult = true;
                }
                else
                {
                    // Set exception so caller can inspect what went wrong.
                    _loadException = ex;
                    MessageBox.Show(ex.Message, "Error creating chart");
                    this.DialogResult = false;
                }

                this.Close();
            }
            else
            {
                this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                    new GenerateCompletionHandler(_headLinxGen_GenerateCompletion),
                    user, new object[] { ex });
            }
        }

        private void _headLinxGen_StatusMessage(string message)
        {
            if (this.Dispatcher.Thread == Thread.CurrentThread)
            {
                tbStatus.Text = message;
            }
            else
            {
                this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
                    new StatusMessageHandler(_headLinxGen_StatusMessage), message);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                _rootUser = null;
                _loadException = null;
                _headLinxGen.ShowPeers = this.ShowPeers;
                _headLinxGen.ShowDirectReports = this.ShowDirectReports;
                _headLinxGen.ForcePhotoCacheRefresh = this.ForcePhotoCacheRefresh;
                _headLinxGen.ShowPhotos = this.ShowPhotos;
                _headLinxGen.PhotoCache = _photoCache;
                _headLinxGen.SelectedUserNames.ParseList(this.SelectedNames);
                
                // Kick off the asynchronous query.
                _headLinxGen.QueryAsync(_queryType);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error creating chart");
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            _headLinxGen.CancelAsync();
        }
    }
}
